﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QLHD.BLL;
using QLHD.DAL;

namespace QLHD
{
    public partial class frmMaTP : Form
    {
        public frmMaTP() { }
        public frmMaTP(frmMain form)
        {            
            InitializeComponent();
            MdiParent = form;
        }

        private void frmMaTP_KT_Load(object sender, EventArgs e)
        {
            dgvMaTP_KT.DataSource = MaTP_DB.SelectMaTP();
            txtMaTP.Text = dgvMaTP_KT[0, 0].Value.ToString();
            txtDienGiai.Text = dgvMaTP_KT[1,0].Value.ToString();
            ReadOnlyTextbox();
        }
        private void NotReadOnlyTextbox()
        {
            txtDienGiai.ReadOnly = false;
            txtMaTP.ReadOnly = false;
        }
        private void ReadOnlyTextbox()
        {
            txtDienGiai.ReadOnly = true;
            txtMaTP.ReadOnly = true;
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            if (btnThem.Text == "Thêm")
            {
                btnThem.Text = "Lưu";
                txtDienGiai.Clear();
                txtMaTP.Clear();
                NotReadOnlyTextbox();
            }
            else
            {
                MaTP item = new MaTP(txtMaTP.Text, txtDienGiai.Text);
                MaTP_DB.Insert(item);
                btnThem.Text = "Thêm";
                frmMaTP_KT_Load(sender, e);
            }
            
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            MaTP_DB.Delete(txtMaTP.Text);
            frmMaTP_KT_Load(sender, e);
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (btnSua.Text == "Sửa")
            {
                NotReadOnlyTextbox();
                btnSua.Text = "Lưu";
            }
            else
            {
                try
                {
                    MaTP item = new MaTP(txtMaTP.Text, txtDienGiai.Text);
                    MaTP_DB.Update(txtMaTP.Text, item);
                    btnSua.Text = "Sửa";
                    frmMaTP_KT_Load(sender, e);
                }
                catch (Exception)
                {
                    return;
                }
            }

        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            frmMaTP_KT_Load(sender, e);
            btnThem.Text = "Thêm";
            btnSua.Text = "Sửa";
        }

        private void dgvMaTP_KT_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                txtMaTP.Text = dgvMaTP_KT[0, e.RowIndex].Value.ToString();
                txtDienGiai.Text = dgvMaTP_KT[1, e.RowIndex].Value.ToString();
            }
            catch (Exception)
            {
                return;
            }
           
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
